package com.ruoyi.art.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.art.domain.GoodSort;
import com.ruoyi.art.service.IGoodSortService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.art.domain.ArtGood;
import com.ruoyi.art.service.IArtGoodService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品表Controller
 * 
 * @author hxm
 * @date 2025-06-09
 */
@RestController
@RequestMapping("/artGood/artGood")
public class ArtGoodController extends BaseController
{
    @Autowired
    private IArtGoodService artGoodService;
    @Autowired
    private IGoodSortService goodSortService;

    /**
     * 查询商品表列表
     */
    @PreAuthorize("@ss.hasPermi('artGood:artGood:list')")
    @GetMapping("/list")
    public TableDataInfo list(ArtGood artGood)
    {
        startPage();
        List<ArtGood> list = artGoodService.selectArtGoodList(artGood);
        System.out.println("这是返回的list" + list);
        return getDataTable(list);
    }

    /**
     * 导出商品表列表
     */
    @PreAuthorize("@ss.hasPermi('artGood:artGood:export')")
    @Log(title = "商品表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ArtGood artGood)
    {
        List<ArtGood> list = artGoodService.selectArtGoodList(artGood);
        ExcelUtil<ArtGood> util = new ExcelUtil<ArtGood>(ArtGood.class);
        util.exportExcel(response, list, "商品表数据");
    }

    /**
     * 获取商品表详细信息
     */
    @PreAuthorize("@ss.hasPermi('artGood:artGood:query')")
    @GetMapping(value = "/{goodId}")
    public AjaxResult getInfo(@PathVariable("goodId") Long goodId)
    {
        return success(artGoodService.selectArtGoodByGoodId(goodId));
    }

    /**
     * 新增商品表
     */
    @PreAuthorize("@ss.hasPermi('artGood:artGood:add')")
    @Log(title = "商品表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ArtGood artGood)
    {
        return toAjax(artGoodService.insertArtGood(artGood));
    }

    /**
     * 修改商品表
     */
    @PreAuthorize("@ss.hasPermi('artGood:artGood:edit')")
    @Log(title = "商品表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ArtGood artGood)
    {
        return toAjax(artGoodService.updateArtGood(artGood));
    }

    /**
     * 删除商品表
     */
    @PreAuthorize("@ss.hasPermi('artGood:artGood:remove')")
    @Log(title = "商品表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{goodIds}")
    public AjaxResult remove(@PathVariable Long[] goodIds)
    {
        return toAjax(artGoodService.deleteArtGoodByGoodIds(goodIds));
    }

    @PostMapping("/moveUp")
    public AjaxResult moveUp(@RequestBody Long goodId) { // 注意：@RequestBody 用于接收 JSON 格式的请求体
        return toAjax(artGoodService.moveUp(goodId));
    }

    @PostMapping("/moveDown")
    public AjaxResult moveDown(@RequestBody Long goodId) {
        return toAjax(artGoodService.moveDown(goodId));
    }


    /**
     * 获取商品类型下拉框数据
     */
    @GetMapping("/selectAllGoodSort")
    public AjaxResult selectAllGoodSort() {
        GoodSort goodSort = new GoodSort();
        List<GoodSort> goodSortList = goodSortService.selectGoodSortList(goodSort);
        return AjaxResult.success(goodSortList);
    }

    /**
     * 搜索商品类型（用于下拉框过滤）
     */
    @GetMapping("/searchGoodSort")
    public AjaxResult searchGoodSort(GoodSort goodSort) {
        List<GoodSort> goodSortList = goodSortService.selectGoodSortList(goodSort);
        return AjaxResult.success(goodSortList);
    }

    // ================= 分销商品相关接口 =================

    /**
     * 查询本校区的所有商品（用于分销商品组件）
     */
    @GetMapping("/ownCampusGoods")
    public AjaxResult getOwnCampusGoods(@RequestParam("campusId") Long campusId) {
        ArtGood artGood = new ArtGood();
        artGood.setCampusId(campusId);
        List<ArtGood> list = artGoodService.selectArtGoodList(artGood);
        return AjaxResult.success(list);
    }

    /**
     * 查询指定校区的分销商品列表
     */
    @GetMapping("/distributionGoods")
    public AjaxResult getDistributionGoods(@RequestParam("campusId") Long campusId) {
        ArtGood artGood = new ArtGood();
        artGood.setCampusId(campusId);
        artGood.setIsDistribution(0L); // 0表示支持分销
        List<ArtGood> list = artGoodService.selectArtGoodList(artGood);
        return AjaxResult.success(list);
    }

    /**
     * 查询所有分销商品（跨校区）
     */
    @GetMapping("/allDistributionGoods")
    public TableDataInfo getAllDistributionGoods(ArtGood artGood) {
        startPage();
        artGood.setIsDistribution(0L); // 只查询支持分销的商品
        List<ArtGood> list = artGoodService.selectArtGoodList(artGood);
        return getDataTable(list);
    }

    /**
     * 分销商品搜索
     */
    @GetMapping("/searchDistribution")
    public AjaxResult searchDistributionGoods(ArtGood artGood) {
        artGood.setIsDistribution(0L); // 只查询支持分销的商品
        List<ArtGood> list = artGoodService.selectArtGoodList(artGood);
        return AjaxResult.success(list);
    }
}
